package com.mvp.shq.washer.ui.shop.product;

import com.mvp.shq.washer.entity.result.BaseResult;
import com.mvp.shq.washer.entity.shop.ShopSpec;
import com.mvp.shq.washer.entity.shop.result.ProductResult;
import com.mvp.shq.washer.http.BaseObserver;
import com.mvp.shq.washer.http.RxSchedulers;
import com.mvp.shq.washer.utils.T;

import java.util.List;
import java.util.Map;

/**
 * Created by shq1 on 2017/10/9.
 */

public class Presenter extends ProductConstract.Presenter {
    @Override
    public void onStart() {

    }

    @Override
    void getSpecByCategoryId(String categoryId) {
        model.getSpecByCategoryId(categoryId)
                .compose(RxSchedulers.io_main())
                .subscribe(new BaseObserver<List<ShopSpec>>(view) {
                    @Override
                    protected void onSuccees(BaseResult<List<ShopSpec>> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.getSpecS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                        e.printStackTrace();
                    }
                });
    }

    @Override
    void getSkuList(Map<String, String> params,int pageNo,int pageSize) {
        model.getSkuList(params,pageNo,pageSize)
                .compose(getActivityLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<ProductResult>(view) {
                    @Override
                    protected void onSuccees(BaseResult<ProductResult> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.getSkuList(t.getData().getList());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show(e.getMessage());
                        e.printStackTrace();
                    }
                });
    }
}
